package com.zrq.recyclerview;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    private DemoAdapter mAdapter;

    /**
     * 颜色数组
     */
    int colors[] = {android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_red_dark};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        //设置网格布局
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        /**
         * 区分Item的类型，横跨多少
         */
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type=mRecyclerView.getAdapter().getItemViewType(position);
                if(type==DataModel.TYPE_THREE){
                    return gridLayoutManager.getSpanCount();
                }else {
                    return 1;
                }
            }
        });
        mRecyclerView.setLayoutManager(gridLayoutManager);

        //设置RecyclerView的样式
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
//                LinearLayoutManager.VERTICAL, false));

        mAdapter = new DemoAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        /**
         * 设置分隔
         */
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
               GridLayoutManager.LayoutParams layoutParams=(GridLayoutManager.LayoutParams)view.getLayoutParams();
                int spansize=layoutParams.getSpanSize();
                int spanIndex=layoutParams.getSpanIndex();
                outRect.top=20;
                if(spansize!=gridLayoutManager.getSpanCount()){
                    if(spanIndex==1){
                        outRect.left=10;
                    }else {
                        outRect.right=10;
                    }
                }
            }
        });

        initData();
    }

    /**
     * 数据源
     */
    private void initData() {
        List<DataModel> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            //使用随机数来选择type
//            int type = (int) ((Math.random() * 3) + 1);
            int type;

            if (i < 5 || (i > 15 && i < 20)) {
                type = 1;
            } else if (i < 10 || i > 26) {
                type = 2;
            } else {
                type = 3;
            }
            DataModel data = new DataModel();
            data.avatarColor = colors[type - 1];
            data.type = type;
            Log.e("TAG", "type==" + type);
            data.name = "name:" + i;
            data.content = "content：" + i;
            data.contentColor = colors[(type + 1) % 3];
            list.add(data);
        }


        List<DataModelOne> list1=new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            DataModelOne data = new DataModelOne();
            data.avatarColor = colors[0];
            data.name = "name:" + i;
            list1.add(data);
        }

        List<DataModelTwo> list2=new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            DataModelTwo data = new DataModelTwo();
            data.avatarColor = colors[1];
            data.name = "name:" + i;
            data.content="content:"+i;
            list2.add(data);
        }

        List<DataModelThree> list3=new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            DataModelThree data = new DataModelThree();
            data.avatarColor = colors[2];
            data.name = "name:" + i;
            data.content="content:"+i;
            data.contentColor=colors[2];
            list3.add(data);
        }



        //适配器加载数据源
        mAdapter.addList(list1,list2,list3);
        //刷新
        mAdapter.notifyDataSetChanged();
    }
}
